<?php

namespace app\common;

use think\App;
use think\Exception;
use think\log\driver\File;

/**自定义日志驱动
 * Class VdLog Driver
 * @author sjm
 * @package app\vdsns\log
 */

class RedisLog  extends File
{

    /**
     * 对应的level
     */
    const DE_EMERGENCY = 'emergency';
    const DE_ALERT     = 'alert';
    const DE_CRITICAL  = 'critical';
    const DE_ERROR     = 'error';
    const DE_WARNING   = 'warning';
    const DE_NOTICE    = 'notice';
    const DE_INFO      = 'info';
    const DE_DEBUG     = 'debug';
    const DE_SQL       = 'sql';

    /**
     * @var bool
     */
    protected $save_db = true;

    /**
     * VdLog constructor.
     * @param mixed ...$args
     */
    public function __construct(App $app, $config = [])
    {
        parent::__construct($app, $config);

        //cli模式日志不入库
        // if (PHP_SAPI == 'cli') {
        //     $this->save_db = false;
        // }
    }

    /**
     * @param array $log
     * @param bool $append
     * @return bool
     */
    public function save(array $log): bool
    {
        //TODO::自定义写入规则
        //$this->intoSave($log);
        return parent::save($log);
    }

    /**
     * @param array $message
     * @param string $destination
     * @param bool $apart
     * @param bool $append
     * @return bool
     */
    public function write(array $message, string $destination): bool
    {
        $this->intoWrite($message, $destination);
        return parent::write($message, $destination);
    }

    /**
     * @param $log
     */
    protected function intoSave($log)
    {
        $this->intoDB($log);
    }

    /**
     * @param $message
     * @param $destination
     * @param bool $apart
     * @param bool $append
     */
    protected function intoWrite($message, $destination, $apart = false, $append = false)
    {
        $log = [
            'message'     => $message,
            'destination' => $destination,
            'apart'       => $apart,
            'append'      => $append,
        ];
        //TODO::自定义写入规则
        if (isset($message['error'])) {
            //只记录错误
            $this->intoDB($log);
        }
    }

    /**
     * @param $log
     */
    protected function intoDB($log)
    {
        if ($this->save_db) {
            $ruleLog = $this->dbLogRule($log);
            if ($ruleLog) {
                try {
                    // 对应日志表的模型
                    Model::create($this->dbLogRule($log));
                } catch (Exception $exception) {
                }
            }
        }
    }

    /** 规则
     * @param $log
     * @return array
     */
    protected function dbLogRule($log)
    {
        //TODO::定义模型规则
        $data = [
            'logs'        => json_encode($log, true),
            'create_time' => date('Y-m-d H:i:s'),
        ];
        return $data;
    }
}
